package com.onlinetp.util;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import org.springframework.util.Base64Utils;

import java.io.UnsupportedEncodingException;
import java.security.Key;
import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.HashMap;
import java.util.Map;

public class RSAUtil {

    public static final String PB_KEY = "pb_key";
    public static final String PR_KEY = "pr_key";

    public static final String CLI_PB_KEY = "cli_pb_key";

    /**
     * 获取公私秘钥对
     * @return
     */
    public static Map<String, Key> getRSAKey(){
        KeyPair pair = SecureUtil.generateKeyPair("RSA");
        PrivateKey privateKey = pair.getPrivate();
        PublicKey publicKey = pair.getPublic();

        Map<String, Key> keys = new HashMap<>();
        keys.put(PR_KEY,privateKey);
        keys.put(PB_KEY,publicKey);

        return keys;
    }

    /**
     * 公钥加密
     * @param pbKey
     * @param content
     * @return
     */
    public static String encByPbKey(String pbKey,String content){
        try {
            byte[] bytes = Base64Utils.decode(pbKey.getBytes("UTF-8"));
            RSA rsa = new RSA(null,bytes);
            byte[] enc = rsa.encrypt(content.getBytes("UTF-8"), KeyType.PublicKey);
            String s = new String(Base64Utils.encode(enc));
            return s;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 公钥加密
     * @param pbKey
     * @param content
     * @return
     */
    public static String encByPbKey(PublicKey pbKey,String content){
        try {
            RSA rsa = new RSA(null,pbKey);
            byte[] enc = rsa.encrypt(content.getBytes("UTF-8"), KeyType.PublicKey);
            String s = new String(Base64Utils.encode(enc));
            return s;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 私钥解密
     * @param prKey
     * @param content
     * @return
     */
    public static String dencByPrKey(String prKey,String content){
        try {
            byte[] bytes = Base64Utils.decode(prKey.getBytes("UTF-8"));
            RSA rsa = new RSA(bytes,null);
            byte[] denc = rsa.decrypt(Base64Utils.decode(content.getBytes("UTF-8")), KeyType.PrivateKey);
            String s = new String(Base64Utils.encode(denc));
            return s;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 私钥解密
     * @param prKey
     * @param content
     * @return
     */
    public static String dencByPrKey(PrivateKey prKey,String content){
        try {
            RSA rsa = new RSA(prKey,null);
            byte[] denc = rsa.decrypt(Base64Utils.decode(content.getBytes("UTF-8")), KeyType.PrivateKey);
            String s = new String(denc);
            return s;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

}
